﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TVShows_EF5.Models;

namespace TVShows_EF5.Controllers.Admin
{
    public class TVShowController : Controller
    {
        private TVShowsContext db = new TVShowsContext();

        //
        // GET: /TVShow/

        public ActionResult Index(string term)
        {
            if (term == null || term == "")
            {
                var resultado = db.TVShows;
                return View(resultado.ToList());
            }
            else
            {
                var resultado = from tvs in db.TVShows
                                where tvs.nombre.Contains(term)
                                select tvs;
                return View(resultado.ToList());
            }


            
        }

        //
        // GET: /TVShow/Details/5

        public ActionResult Details(long id = 0)
        {
            TVShow tvshow = db.TVShows.Find(id);
            if (tvshow == null)
            {
                return HttpNotFound();
            }
            return View(tvshow);
        }

        //
        // GET: /TVShow/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /TVShow/Create

        [HttpPost]
        public ActionResult Create(TVShow tvshow)
        {
            if (ModelState.IsValid)
            {
                db.TVShows.Add(tvshow);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tvshow);
        }

        //
        // GET: /TVShow/Edit/5

        public ActionResult Edit(long id = 0)
        {
            TVShow tvshow = db.TVShows.Find(id);
            if (tvshow == null)
            {
                return HttpNotFound();
            }
            return View(tvshow);
        }

        //
        // POST: /TVShow/Edit/5

        [HttpPost]
        public ActionResult Edit(TVShow tvshow)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tvshow).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tvshow);
        }

        //
        // GET: /TVShow/Delete/5

        public ActionResult Delete(long id = 0)
        {
            TVShow tvshow = db.TVShows.Find(id);
            if (tvshow == null)
            {
                return HttpNotFound();
            }
            return View(tvshow);
        }

        //
        // POST: /TVShow/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            TVShow tvshow = db.TVShows.Find(id);
            db.TVShows.Remove(tvshow);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}